package com.credithc.customers.dataservice.service.impl;

import com.credithc.customers.base.constants.CustomerConstants;
import com.credithc.customers.base.utils.CustomerUtils;
import com.credithc.customers.dataservice.Thread.OldDataThreadPoolExecutor;
import com.credithc.customers.dataservice.constants.SyncTypeEnum;
import com.credithc.customers.dataservice.mapper.XT.EntrustUserMapper;
import com.credithc.customers.dataservice.po.GdAccountPo;
import com.credithc.customers.dataservice.ro.CustomerAccountInfoRo;
import com.credithc.customers.dataservice.service.OldXtDataService;
import com.credithc.customers.dataservice.service.SyncDataService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicReference;

@Service
@Slf4j
public class OldXtDataServiceImpl implements OldXtDataService {
    @Autowired
    private EntrustUserMapper entrustUserMapper;

    @Autowired
    private SyncDataService syncDataService;
    @Autowired
    private OldDataThreadPoolExecutor oldDataThreadPoolExecutor;

    @Override
    public void oldData() {
        boolean f =true;
        Long num = entrustUserMapper.queryProductMaxId();
        if (num == null){
            return;
        }
        AtomicReference<Long> vernier = new AtomicReference<>( num+1001);
        ExecutorService e = oldDataThreadPoolExecutor.getCustomThreadPoolExecutor();
        while (f) {
            e.submit(() -> {
                try {
                    if (vernier.get()>0){
                        vernier.set(vernier.get()-1000);
                    }
                    List<GdAccountPo> gdCustomerProductPOS = entrustUserMapper.queryDlrProduct(Long.toString(vernier.get()));
                    if (gdCustomerProductPOS.size() > 0) {
                        bulidXtAccount(gdCustomerProductPOS);
                    }
                } catch (Exception e1) {
                    log.error(e1.getMessage());
                }
            });
            f = OldHyrDataServiceImpl.exit(f, vernier, e);
        }
    }

    private void bulidXtAccount(List<GdAccountPo> gdCustomerProductPOS) {

        List<CustomerAccountInfoRo> customerAccountInfoRos = new ArrayList<>();
        for (GdAccountPo xd:gdCustomerProductPOS) {
            CustomerAccountInfoRo customerAccountInfoRo = new CustomerAccountInfoRo();
            customerAccountInfoRo.setAccountType(CustomerConstants.CUSTOMER_ACCOUNT_TYPE_BANK_CARD);
            customerAccountInfoRo.setSubSystemSign(xd.getSubSystemSign()+"XT");
            customerAccountInfoRo.setAccountNo(xd.getAccountNo());
            customerAccountInfoRo.setUserSourceId(xd.getUserSourceId());
            customerAccountInfoRo.setSystemSign("XT");
            customerAccountInfoRo.setSyncType(SyncTypeEnum.NEW.getValue());
            customerAccountInfoRo.setAccountUpdateTime(StringUtils.isNotBlank(xd.getAccountUpdateTime())? CustomerUtils.dateStr2Timestamp(xd.getAccountUpdateTime()):null );
            customerAccountInfoRo.setAccountCreateTime(StringUtils.isNotBlank(xd.getAccountCreateTime())?CustomerUtils.dateStr2Timestamp(xd.getAccountCreateTime()):null);
            customerAccountInfoRo.setCustomerNo(xd.getCustomerNo());
            customerAccountInfoRo.setCustomerRole(CustomerConstants.CUSTOMER_ACCOUNT_TYPE_LOAN);
            customerAccountInfoRo.setAccountStatus(xd.getBind_state() == 300 ?CustomerConstants.STATUS_ACCOUNT_AVAILABLE:CustomerConstants.STATUS_ACCOUNT_UNAVAILABLE);
            //customerAccountInfoRo.setAccountStatus(StringUtils.isNotBlank(xd.getAccountNo()) ? CustomerConstants.STATUS_ACCOUNT_AVAILABLE:CustomerConstants.STATUS_ACCOUNT_UNAVAILABLE);
            if (StringUtils.isNotBlank(customerAccountInfoRo.getCustomerNo()) && !"0".equals(customerAccountInfoRo.getCustomerNo())) {
                customerAccountInfoRos.add(customerAccountInfoRo);
            }
        }
        syncDataService.syncCustomerAccount(customerAccountInfoRos);
    }
}
